﻿using Iworld.Administrator.Model;
using Iworld.Author.Model;
using Iworld.Basis.Model;
using Iworld.Game.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Main.Impl
{
    /// <summary>
    /// 数据库对象
    /// </summary>
    public class ModelToDbContext : DbContext, IModelToDbContext,IModelToDbContextOfAuthor,IModelToDbContextOfGame,IModelToDbContextOfAdministrator
    {
        /// <summary>
        /// 用户信息的数据存储区
        /// </summary>
        public DbSet<Iworld.Author.Model.Author> Authors { get; set; }

        /// <summary>
        /// 用户金币变化记录数据存储区
        /// </summary>
        public DbSet<GoldChangeRecord> GoldChangeRecords { get; set; }

        /// <summary>
        /// 充值卡信息数据存储区
        /// </summary>
        public DbSet<RechargeCard> RechargeCards { set; get; }

        /// <summary>
        /// 用户登陆记录的数据存储区
        /// </summary>
        public DbSet<AuthorLandingRecord> AuthorLandingRecords { get; set; }

        /// <summary>
        /// 网络 - 实际地址对照的数据存储区
        /// </summary>
        public DbSet<IpToAddressComparison> IpToAddressComparisons { get; set; }

 
        /// <summary>
        /// 相关设定的明细的数据存储区
        /// </summary>
        public DbSet<SettingDetail> SettingDetails { get; set; }

        /// <summary>
        /// 赠送金币记录数据储存区
        /// </summary>
        public DbSet<GiveGoldsRecord> GiveGoldsRecords { get; set; }

        /// <summary>
        /// 牌轮信息存储区
        /// </summary>
        public DbSet<GameChapter> GameChapters { get; set; }

        /// <summary>
        /// 牌局信息储存区
        /// </summary>
        public DbSet<GameRound> GameRounds { set; get; }

        /// <summary>
        /// 投注信息储存区
        /// </summary>
        public DbSet<Betting> Bettings { set; get; }

         /// <summary>
        /// 管理员的数据存储区
        /// </summary>
        public DbSet<Iworld.Administrator.Model.Administrator> Administrators { get; set; }

        /// <summary>
        /// 管理员用户组的数据存储区
        /// </summary>
        public DbSet<AdministratorGroup> AdministratorGroups { get; set; }

        /// <summary>
        /// 管理员登陆记录的数据存储区
        /// </summary>
        public DbSet<AdministratorLandingRecord> AdministratorLandingRecords { get; set; }

        /// <summary>
        /// 用户的登陆信息的数据存储区
        /// </summary>
        public DbSet<AuthorLoginInfo> AuthorLoginInfos { get; set; }

        /// <summary>
        /// 管理员的登陆信息的数据存储区
        /// </summary>
        public DbSet<AdministratorLoginInfo> AdministratorLoginInfos { get; set; }

        /// <summary>
        /// 提现
        /// </summary>
        public DbSet<WithdrawalsRecord> WithdrawalsRecords { get; set; }
        
        public ModelToDbContext()
        {
           
        }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {    
            #region 基础
               modelBuilder.Entity<IpToAddressComparison>().ToTable("iworld-IpToAddressComparison").HasKey(x => x.Id);

               modelBuilder.Entity<SettingDetail>().ToTable("iworld-SettingDetail").HasKey(x => x.Id);
            #endregion

               #region 管理员
               modelBuilder.Entity<Iworld.Administrator.Model.AdministratorGroup>().ToTable("iworld-administrator-group");
               modelBuilder.Entity<Iworld.Administrator.Model.Administrator>().ToTable("iworld-administrator").HasOptional(x=>x.Group);

               modelBuilder.Entity<AdministratorLandingRecord>().ToTable("iworld-AdministratorLandingRecord");
               modelBuilder.Entity<AdministratorLandingRecord>().HasKey(x => x.Id);

               modelBuilder.Entity<AdministratorLandingRecord>().HasOptional(x => x.Owner);
               #endregion

               #region 用户储存区域

               modelBuilder.Entity<Iworld.Author.Model.Author>().ToTable("iworld-Author").HasKey(x => x.Id)
               .HasMany(x => x.Banks).WithOptional(x => x.Owner);

                modelBuilder.Entity<AuthorBank>().ToTable("iworld-AuthorBank").HasKey(x => x.Id);

                modelBuilder.Entity<AuthorLandingRecord>().ToTable("iworld-AuthorLandingRecord").HasKey(x => x.Id).HasOptional(x => x.Owner);

                modelBuilder.Entity<GoldChangeRecord>().ToTable("iworld-GoldChangeRecord").HasKey(x => x.Id).HasOptional(x => x.Owner);

                modelBuilder.Entity<RechargeCard>().ToTable("iworld-RechargeCard").HasKey(x => x.Id).HasOptional(x => x.UsedAuthor);

                modelBuilder.Entity<AuthorLoginInfo>().ToTable("iworld-AuthorLoginInfo");
                modelBuilder.Entity<AuthorLoginInfo>().HasKey(x => x.Id);

                modelBuilder.Entity<GiveGoldsRecord>().ToTable("iworld-GiveGoldsRecord").HasKey(x => x.Id).HasOptional(x => x.From);
                modelBuilder.Entity<GiveGoldsRecord>().HasOptional(x => x.To);

                modelBuilder.Entity<WithdrawalsRecord>().ToTable("iworld-WithdrawalsRecord").HasKey(x => x.Id).HasOptional(x => x.Owner);
                
            #endregion

            #region 游戏区
                modelBuilder.Entity<GameChapter>().ToTable("iworld-GameChapter").HasKey(x => x.Id)
                .HasMany(x => x.GameRounds).WithOptional(x => x.GameChapter);
              /*  modelBuilder.Entity<GameChapter>().HasOptional(x => x.CurrentGameRound);
                modelBuilder.Entity<GameChapter>().HasOptional(x => x.NextGameRound);*/
                modelBuilder.Entity<GameRound>().ToTable("iworld-GameRound").HasKey(x=>x.Id);

                modelBuilder.Entity<Betting>().ToTable("iworld-Betting").HasKey(x => x.Id);
                modelBuilder.Entity<Betting>().HasOptional(x => x.Owner);
            #endregion

           base.OnModelCreating(modelBuilder);
        }


    }
}
